﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using AGR.Web.Shared;
using System.Web.Security;

namespace AGR.Web
{
    public partial class SiteMaster : System.Web.UI.MasterPage
    {
        private CurrentUser _currentUser;

        protected void Page_Init(object sender, EventArgs e)
        {
            _currentUser = Session["CurrentUser"] as CurrentUser;

            if (_currentUser == null)
            {
                if (Session.IsNewSession)
                {
                    // If it says it is a new session, but an existing cookie exists, then it must 
                    // have timed out (can't use the cookie collection because even on first 
                    // request it already contains the cookie (request and response
                    // seem to share the collection)
                    string cookieHeader = Request.Headers["Cookie"];

                    if ((null != cookieHeader) && (cookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
                    {
                        ValidateUser();
                        ////throw new Exception("SESSION EXPIRED");
                    }
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

            }
        }

        #region Event Methods

        protected void LoginUser_OnLoggedIn(object sender, EventArgs e)
        {

        }

        protected void LoginStatus_OnLoggedOut(object sender, EventArgs e)
        {

        }

        protected void lnkInformationClose_Click(object sender, EventArgs e)
        {
            this.ClearMessage();
        }

        protected void lnkErrorClose_Click(object sender, EventArgs e)
        {
            this.ClearMessage();
        }

        protected void lnkWarningClose_Click(object sender, EventArgs e)
        {
            this.ClearMessage();
        }

        protected void lnkSuccessClose_Click(object sender, EventArgs e)
        {
            this.ClearMessage();
        }

        #endregion

        #region Public Methods

        public enum MessageType
        {
            Error,
            Success,
            Warning,
            Information
        }

        public void ShowMessage(string message, MessageType messageType)
        {
            Label warningMessageLabel = (Label)Page.Master.FindControl("warningMessageLabel");
            Label errorMessageLabel = (Label)Page.Master.FindControl("errorMessageLabel");
            Label successMessageLabel = (Label)Page.Master.FindControl("successMessageLabel");
            Label informationMessageLabel = (Label)Page.Master.FindControl("informationMessageLabel");

            Panel warningMessage = (Panel)Page.Master.FindControl("warningMessage");
            Panel errorMessage = (Panel)Page.Master.FindControl("errorMessage");
            Panel informationMessage = (Panel)Page.Master.FindControl("informationMessage");
            Panel successMessage = (Panel)Page.Master.FindControl("successMessage");

            if (messageType == MessageType.Error)
            {
                errorMessage.Visible = true;
                errorMessageLabel.Text = message.ToString();

                informationMessage.Visible = false;
                informationMessageLabel.Text = string.Empty;
                successMessage.Visible = false;
                successMessageLabel.Text = string.Empty;
                warningMessage.Visible = false;
                warningMessageLabel.Text = string.Empty;
            }
            else if (messageType == MessageType.Information)
            {
                informationMessage.Visible = true;
                informationMessageLabel.Text = message.ToString();

                successMessage.Visible = false;
                successMessageLabel.Text = string.Empty;
                warningMessage.Visible = false;
                warningMessageLabel.Text = string.Empty;
                errorMessage.Visible = false;
                errorMessageLabel.Text = string.Empty;
            }
            else if (messageType == MessageType.Success)
            {
                successMessage.Visible = true;
                successMessageLabel.Text = message.ToString();

                informationMessage.Visible = false;
                informationMessageLabel.Text = string.Empty;
                warningMessage.Visible = false;
                warningMessageLabel.Text = string.Empty;
                errorMessage.Visible = false;
                errorMessageLabel.Text = string.Empty;
            }
            else
            {
                if (messageType == MessageType.Warning)
                {
                    warningMessage.Visible = true;
                    warningMessageLabel.Text = message.ToString();

                    successMessage.Visible = false;
                    successMessageLabel.Text = string.Empty;
                    informationMessage.Visible = false;
                    informationMessageLabel.Text = string.Empty;
                    errorMessage.Visible = false;
                    errorMessageLabel.Text = string.Empty;
                }
            }
        }

        public void ClearMessage()
        {
            Label warningMessageLabel = (Label)Page.Master.FindControl("warningMessageLabel");
            Label errorMessageLabel = (Label)Page.Master.FindControl("errorMessageLabel");
            Label successMessageLabel = (Label)Page.Master.FindControl("successMessageLabel");
            Label informationMessageLabel = (Label)Page.Master.FindControl("informationMessageLabel");

            Panel warningMessage = (Panel)Page.Master.FindControl("warningMessage");
            Panel errorMessage = (Panel)Page.Master.FindControl("errorMessage");
            Panel informationMessage = (Panel)Page.Master.FindControl("informationMessage");
            Panel successMessage = (Panel)Page.Master.FindControl("successMessage");

            warningMessage.Visible = false;
            warningMessageLabel.Text = string.Empty;
            successMessage.Visible = false;
            successMessageLabel.Text = string.Empty;
            informationMessage.Visible = false;
            informationMessageLabel.Text = string.Empty;
            errorMessage.Visible = false;
            errorMessageLabel.Text = string.Empty;
        }
        #endregion

        #region Private Methods

        private void ValidateUser()
        {
            if (_currentUser != null)
            {
                Session["CurrentUser"] = _currentUser;
            }
            else
            {
                _currentUser = new CurrentUser();
            }
        }
        #endregion
    }
}
